<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="影视名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入影视名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年份" clearable>
          <el-option
            v-for="year in yearList"
            :key="year"
            :label="year"
            :value="year"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="columnValue">
        <el-select v-model="queryParams.columnValue" placeholder="请选择类型" clearable>
          <el-option label="电影" value="movie" />
          <el-option label="电视剧" value="tv" />
          <el-option label="动漫" value="cartoon" />
          <el-option label="综艺" value="variety" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['movie:basics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['movie:basics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['movie:basics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['movie:basics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basicsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="影视ID" align="center" prop="movieBasicsId" width="80" />
      <el-table-column label="海报" align="center" prop="poster" width="100">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 80px"
            :src="scope.row.poster"
            :preview-src-list="[scope.row.poster]"
            fit="cover"
            v-if="scope.row.poster"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><Picture /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="影视名称" align="center" prop="title" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="首字母" align="center" prop="letter" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.letter" type="info">{{ scope.row.letter }}</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" prop="year" width="80" />
      <el-table-column label="类型" align="center" prop="columnValue" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.columnValue === 'movie'">电影</el-tag>
          <el-tag type="success" v-else-if="scope.row.columnValue === 'tv'">电视剧</el-tag>
          <el-tag type="info" v-else-if="scope.row.columnValue === 'cartoon'">动漫</el-tag>
          <el-tag type="warning" v-else-if="scope.row.columnValue === 'variety'">综艺</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="score" width="150">
        <template #default="scope">
          <el-rate
            v-model="scope.row.score"
            disabled
            :max="5"
            :allow-half="true"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
          />
        </template>
      </el-table-column>
      <el-table-column label="播放量" align="center" prop="hits" width="100" />
      <el-table-column label="发行状态" align="center" prop="releaseStatus" width="80">
        <template #default="scope">
          <el-switch
            v-model="scope.row.releaseStatus"
            active-value="1"
            inactive-value="0"
            @change="handleRecommendChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="VideoPlay" @click="handleVideos(scope.row)" v-hasPermi="['movie:video:list']">视频</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['movie:basics:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['movie:basics:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改影视对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="basicRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="影视名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入影视名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副标题" prop="subTitle">
              <el-input v-model="form.subTitle" placeholder="请输入副标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="英文名称" prop="titleEn">
              <el-input v-model="form.titleEn" placeholder="请输入英文名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首字母" prop="letter">
              <el-input v-model="form.letter" placeholder="自动生成或手动输入" maxlength="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发行年份" prop="year">
              <el-select v-model="form.year" placeholder="请选择发行年份">
                <el-option
                  v-for="year in yearList"
                  :key="year"
                  :label="year"
                  :value="year"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时长(分钟)" prop="duration">
              <el-input-number v-model="form.duration" :min="0" placeholder="请输入时长" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="影视类型" prop="columnValue">
              <el-select v-model="form.columnValue" placeholder="请选择影视类型">
                <el-option label="电影" value="movie" />
                <el-option label="电视剧" value="tv" />
                <el-option label="动漫" value="cartoon" />
                <el-option label="综艺" value="variety" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评分" prop="score">
              <el-input-number v-model="form.score" :precision="1" :step="0.1" :min="0" :max="10" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="海报图片" prop="poster">
              <el-input v-model="form.poster" placeholder="请输入海报图片URL" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="缩略图" prop="picThumb">
              <el-input v-model="form.picThumb" placeholder="请输入缩略图URL" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="幻灯图" prop="picSlide">
              <el-input v-model="form.picSlide" placeholder="请输入幻灯图URL" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="截图集" prop="picScreenshot">
              <el-input v-model="form.picScreenshot" placeholder="请输入截图集URL，多个用逗号分隔" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="扩展分类" prop="classExtend">
              <el-input v-model="form.classExtend" placeholder="请输入扩展分类标签，多个用逗号分隔" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="剧情简介" prop="summary">
              <el-input
                type="textarea"
                v-model="form.summary"
                placeholder="请输入剧情简介"
                :rows="4"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="推荐" prop="isRecommend">
              <el-radio-group v-model="form.isRecommend">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="热门" prop="isHot">
              <el-radio-group v-model="form.isHot">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最新" prop="isNew">
              <el-radio-group v-model="form.isNew">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MovieBasics">
import { listBasics, getBasics, delBasics, addBasics, updateBasics } from "@/api/movie/basics";
const { proxy } = getCurrentInstance();
const router = proxy.$router;

const basicsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 生成年份列表
const yearList = ref([]);
const currentYear = new Date().getFullYear();
for (let i = currentYear; i >= 1900; i--) {
  yearList.value.push(i);
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    year: null,
    columnValue: null,
    status: null
  },
  rules: {
    title: [
      { required: true, message: "影视名称不能为空", trigger: "blur" }
    ],
    columnValue: [
      { required: true, message: "影视类型不能为空", trigger: "change" }
    ],
    year: [
      { required: true, message: "发行年份不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询影视列表 */
function getList() {
  loading.value = true;
  listBasics(queryParams.value).then(response => {
    basicsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    movieBasicsId: null,
    title: null,
    titleEn: null,
    year: null,
    duration: null,
    columnValue: null,
    summary: null,
    score: 0,
    hits: 0,
    poster: null,
    status: "0",
    isRecommend: "0",
    isHot: "0",
    isNew: "0"
  };
  proxy.resetForm("basicRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.movieBasicsId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加影视";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _movieBasicsId = row.movieBasicsId || ids.value
  getBasics(_movieBasicsId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改影视";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["basicRef"].validate(valid => {
    if (valid) {
      if (form.value.movieBasicsId != null) {
        updateBasics(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBasics(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _movieBasicsIds = row.movieBasicsId || ids.value;
  proxy.$modal.confirm('是否确认删除影视编号为"' + _movieBasicsIds + '"的数据项？').then(function() {
    return delBasics(_movieBasicsIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('movie/basics/export', {
    ...queryParams.value
  }, `movie_basics_${new Date().getTime()}.xlsx`)
}

// 状态修改
function handleStatusChange(row) {
  let text = row.status === "0" ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.title + '"吗？').then(function() {
    return updateBasics(row);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function() {
    row.status = row.status === "0" ? "1" : "0";
  });
}

// 推荐修改
function handleRecommendChange(row) {
  let text = row.releaseStatus === "1" ? "发行" : "取消发行";
  updateBasics(row).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function() {
    row.releaseStatus = row.releaseStatus === "1" ? "0" : "1";
  });
}

// 视频管理
function handleVideos(row) {
  router.push('/movie/video?movieBasicsId=' + row.movieBasicsId);
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 30px;
}
</style>
